#!/usr/bin/env python
# -*- coding: utf-8 -*-

##baseado no codigo fornecido pelo docente

'''
classe para criar e preencher a base de dados
Autor: Sandro Sousa 5217
mail: sandromrsousa@gmail.com
'''


import csv
import sqlite3


class criardb:

    inclines = 0
    count = 0


    def tabela(self):

        conn = sqlite3.connect('rebides.db')

        cursor = conn.cursor()
        cursor.execute('''drop table if exists profs''')
        cursor.execute('''create table profs ( codigo_docente integer,
                        nome text,
                        ultimo_grau text,
                        curso text,
                        categoria text,
                        regime_tempo text,
                        ano_letivo integer,
                        tipo_estabelecimento text,
                        estabelecimento text)''')

        conn.close()
        pass


    def inserir_dados(self):
        
        conn = sqlite3.connect('rebides.db')
        cursor = conn.cursor()

        for numbfile in range(0,10):

            spamReader = csv.reader(open('reb{0}.csv'.format(numbfile), 'rb'), delimiter=',')
            pass

            count = 0
            for row in spamReader:
                if count > 0:
                    cursor.execute('''insert into profs values ("{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}",\
                                    "{7}", "{8}") '''.format(row[0].replace('"','\''), row[1].replace('"','\''),\
                                                             row[2].replace('"','\''), row[3].replace('"','\''),\
                                                             row[4].replace('"','\''), row[5].replace('"','\''),\
                                                             row[6].replace('"','\''), row[7].replace('"','\''),\
                                                             row[8].replace('"','\'')))
                count += 1
                pass
    
        pass
        conn.commit()
        conn.close()


#teste = CriarBd()
#teste.tabela()
#teste.inserir_dados()

